Because the double-ended queue is thread-safe, you can leverage the contents of the queue from both ends in different threads.
Import collectionsimport Threadingimport timecandle = Collections.deque (xrange (5)) def Burn (Direction, Nextsource): While True: try: next = Nextsource () except Indexerror: break else: print '%8s:%s '% ( direction, next) Time.sleep (0.1) print '%8s done '% direction
element from the left side of the deque.
From ' right:
G F E d C b A ' left
:
0 1 2 3 4 5
Because a two-terminal queue is thread-safe, the contents of the queue can be leveraged from both ends in different threads.
Import Collections
Import threading
import time
candle = Collections.deque (xrange (5))
def Burn ( Direction, Nextsource): While
True:
try:
next = Nextsource ()
e
= collections.deque()d2.extendleft(xrange(6))print 'extendleft', d2d2.appendleft(6)print 'appendleft', d2
2. pop/popleft
from collections import collectionsprint "From the right:"d = collections.deque('abcdefg')while True: try: print d.pop(), except IndexError: breakprintprint "\nFrom the left:"d = collections.deque(xrange(6))while True: try: print d.popleft(), except IndexError: breakprint
Because the dual-end queue is thread-safe, the queue content can be used from both ends in different
from the right side of deque, and use popleft () to delete an element from the left side of deque.
From the right:g f e d c b aFrom the left:0 1 2 3 4 5
Because the dual-end queue is thread-safe, the queue content can be used from both ends in different threads at the same time.
import collectionsimport threadingimport timecandle = collections.deque(xrange(5))def burn(direction, nextSource): while True: try: next =
(),exceptIndexerror: Break Print The output is:
From the right
G F E D c B A
From the left
0 1 2 3 4 5
Finally, it is worth mentioning that deque is thread-safe, meaning that you can operate from the left and right side of the Deque collection at the same time without affecting it, see the following code: ImportCollectionsImportThreadingImportTime Candle=collections.deque (Xrange (5)) defBurn (direction,nextsource
Execution Result:Finally, it is worth mentioning that deque is thread-safe, meaning that you can operate from the left and right side of the Deque collection at the same time without affecting it, see the following code:ImportCollectionsImportThreadingImportTime Candle=collections.deque (Xrange (5)) defBurn (direction,nextsource): whileTrue:Try: Next=Nextsource ()exceptIndexerror: Break
Transferred from: http://www.cnblogs.com/wodemeng/archive/2012/04/06/2435302.htmlThis evening friends encounter Web services side push technology problem, I looked up the next information, learning the nextSource code: Http://files.cnblogs.com/wodemeng/AspNetComet.zipComet is sometimes also called reverse Ajax or server-side push technology (server-side push). The idea is simple: push data directly from the server to the browser without waiting for th
Contact Us
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.